package com.educate.ucenter.shiro;

import com.educate.ucenter.shiro.filter.UserFilter;
import com.educate.ucenter.shiro.realm.UserRealm;
import com.educate.ucenter.shiro.seesion.UserSessionFactory;
import com.educate.ucenter.shiro.seesion.UserWebSubjectFactory;
import com.educate.ucenter.shiro.seesion.UserSessionDao;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
@Log4j2
public class ShiroConfig {


//    @Bean
//    public StatelessDefaultSubjectFactory subjectFactory() {
//        return new StatelessDefaultSubjectFactory();
//    }

    @Bean
    public DefaultSessionManager sessionManager(UserSessionDao userSessionDao) {
//        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(false);
        sessionManager.setSessionDAO(userSessionDao);
        sessionManager.setSessionFactory(new UserSessionFactory());
        return sessionManager;
    }


    @Bean
    public DefaultSecurityManager securityManager(SessionManager sessionManager) {

        UserRealm statelessRealm = new UserRealm();
        statelessRealm.setCachingEnabled(false);

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(statelessRealm);
        securityManager.setSubjectFactory(new UserWebSubjectFactory());
        securityManager.setSessionManager(sessionManager);
//        DefaultSubjectDAO subjectDAO = (DefaultSubjectDAO) securityManager.getSubjectDAO();
//        DefaultSessionStorageEvaluator storageEvaluator = (DefaultSessionStorageEvaluator) subjectDAO.getSessionStorageEvaluator();
//        storageEvaluator.setSessionStorageEnabled(false);
        return securityManager;
    }

//    @Bean
//    public FilterRegistrationBean registration(StatelessAuthcFilter filter) {
//        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
//        registration.setEnabled(false);
//        return registration;
//    }

    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        SecurityUtils.setSecurityManager(securityManager);

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, Filter> filters = new HashMap<>();
        filters.put("cuser", new UserFilter());
        shiroFilterFactoryBean.setFilters(filters);
        //拦截器.
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        shiroFilterFactoryBean.setLoginUrl("/sso/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/manager/verify/**", "anon");
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/**", "cuser");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

//    @Bean
//    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
//        return new LifecycleBeanPostProcessor();
//    }
//
//    @Bean
//    public CustomDefaultFilterChainManager customDefaultFilterChainManager() {
//        CustomDefaultFilterChainManager filterChainManager = new CustomDefaultFilterChainManager();
//        filterChainManager.setLoginUrl("/login");
//        filterChainManager.setSuccessUrl("/index");
//        return filterChainManager;
//    }
//
//    @Bean
//    public CustomPathMatchingFilterChainResolver filterChainResolver(CustomDefaultFilterChainManager filterChainManager) {
//        CustomPathMatchingFilterChainResolver filterChainResolver = new CustomPathMatchingFilterChainResolver();
//        filterChainResolver.setCustomDefaultFilterChainManager(filterChainManager);
//        return filterChainResolver;
//    }

//    @Bean
//    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        return shiroFilterFactoryBean;
//    }
//
//    @Bean
//    public MethodInvokingFactoryBean methodInvokingFactoryBean(DefaultWebSecurityManager securityManager,
//                                                               CustomPathMatchingFilterChainResolver filterChainResolver) {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        MethodInvokingFactoryBean mfBean = new MethodInvokingFactoryBean();
//        mfBean.setTargetObject(shiroFilterFactoryBean);
//        mfBean.setTargetMethod("setFilterChainResolver");
//        mfBean.setArguments(filterChainResolver);
//        return mfBean;
//    }


}
